package cn.wink.springcloud.controller;

import cn.wink.springcloud.service.PaymentFeignService;
import cn.wink.springcloud.utils.Result;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author xw
 * @date 2020-05-16 23:32
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrderFeignController {

    @Autowired
    private PaymentFeignService paymentFeignService;

    @GetMapping("/get/{id}")
    public Result get(@PathVariable("id") Long id){
        return paymentFeignService.getOne(id);
    }
    //消费者侧
    @HystrixCommand(fallbackMethod = "fallbackMethod",commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "2000")
    })
    @GetMapping("/get/timeout/{id}")
    public Result getTimeout(@PathVariable("id") Long id){
        // ThreadUtil.sleep(3000);
        return paymentFeignService.getOne(id);
    }

    public Result fallbackMethod(Long id){
        return Result.ok("超时，返回失败，进入降级方法！");
    }

    @GetMapping("/get/circuit/{id}")
    public Result getCircuit(@PathVariable("id") Long id){
        return paymentFeignService.paymentCircuitBreaker(id);
    }
}
